const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
  const Permission = sequelize.define('Permission', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true
  },
  displayName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  resource: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '资源名称'
  },
  action: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '操作类型'
  },
  category: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '权限分类'
  },
  parentId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    references: {
      model: 'Permissions',
      key: 'id'
    },
    comment: '父权限ID'
  },
  level: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '权限层级'
  },
  isSystem: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '是否为系统权限'
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive'),
    defaultValue: 'active'
  }
}, {
  tableName: 'permissions',
  timestamps: true
});

// 自关联
Permission.belongsTo(Permission, { as: 'parent', foreignKey: 'parentId' });
Permission.hasMany(Permission, { as: 'children', foreignKey: 'parentId' });

  return Permission;
};